let d = document;
let login = d.querySelector(".login_form");
let loginBtn = login.querySelector(".btn");
let check = login.querySelector(".checkbox");
let isPC = IsPC();
let uP = JSON.parse(localStorage.getItem("uP")) || [{username: "", password: ""}];
let usr = login.querySelector(".username");
let pwd = login.querySelector(".password");
usr.value = uP[0].username;
pwd.value = uP[0].password;


bind(isPC);

function bind(isPC) {
  if (!isPC) {
    loginBtn.addEventListener("touchend", theLogin);
    return
  }
  loginBtn.addEventListener("click", theLogin);
}

function theLogin() {
  let isT = false;
  if ((usr.value).trim() != "") {
    isT = true
  } else {
    usr.classList.add("wamming")
  }
  if ((pwd.value).trim() != "") {
    isT = true
  } else {
    pwd.classList.add("wamming")
  }
  if (isT) {
    let res = loginStorage({
      username: usr.value.trim(),
      password: pwd.value.trim()
    });
    if (check.checked === true) {
      let uPArr = JSON.parse(localStorage.getItem("uP")) || [{username: "", password: ""}];
      uPArr[0] = {
        username: usr.value.trim(),
        password: pwd.value.trim()
      }
      localStorage.setItem("uP", JSON.stringify(uPArr))
    }
    if (res === 1) {
      tooltip(d.body, {
        message: "登陆成功"
      });
      sessionStorage.setItem("user_session", usr.value.trim());
      usr.value = pwd.value = "";
      setTimeout(() => {
        window.location.href = "index.html"
      }, 2000)
    } else if (res === 2) {
      tooltip(d.body, {
        message: "密码错误"
      })
    } else if (res === 3) {
      tooltip(d.body, {
        message: "用户不存在，先注册"
      })
    }
    
  } else {
    console.log(1)
    tooltip(d.body, {
      message: "账号或密码有误"
    })
  }
}
